/*
 * Copyright Aristool AG Switzerland
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.opprua.dss.data;

import java.util.Map;
import java.util.TreeMap;

/**
 * TODO: add Java doc
 *
 * Prepared for first commit: 23.02.2007
 *
 * @author Lin Li
 */
public class Result<T> {

  public static enum TYPE {
    SUCCESS, FAILURE, PASS, EMPTY
  }

  private TYPE type = TYPE.EMPTY;
  private T data = null;
  private Map<String, String> reason = null;
  
  private Result(TYPE type, T data) {
    this.type = type;
    this.data = data;
  }

  private Result(TYPE type) {
    this.type = type;
  }
  
  private Result(Map<String, String> r) {
    this.reason = r;
    this.type = TYPE.FAILURE;
  }
  
  public static<T> Result<T> success(T data) {
    return new Result<T>(TYPE.SUCCESS, data);
  }
  
  public static<T> Result<T> failure(Map<String, String> o) {
    return new Result<T>(o);
  }
  
  public static<T> Result<T> failure(String r, String o) {
    Map<String, String> m = new TreeMap<String, String>();
    m.put(r, o);
    return new Result<T>(m);
  }
  
  public static<T> Result<T> pass() {
    return new Result<T>(TYPE.PASS);
  }
  
  public static<T> Result<T> empty() {
    return new Result<T>(TYPE.EMPTY);
  }
  
  public TYPE getType() {
    return type;
  }
  
  public T getData() {
    return data;
  }
  
  public Map<String, String> getReason() {
    return reason;
  }
}
